package com.conversationboard.view;

import java.sql.SQLException;

import com.conversationboard.formatter.ImageHider;
import com.conversationboard.model.Message;
import com.conversationboard.model.PollResult;
import com.conversationboard.model.Thread;
import com.conversationboard.model.User;

public class PollResultsDisplay {

	public static String display(String key, Thread thread, User user, boolean mobileDevice) throws SQLException {

		StringBuilder buffer = new StringBuilder();

		buffer.append("<div class=\"poll\">");

		Message message = thread.getFirstMessage();

		buffer.append("<p>");
		buffer.append(message.getBody());
		buffer.append("</p>");

		buffer.append("<h3>Poll Results</h3>");

		buffer.append("<table class='polltable'>");

		buffer.append("<tr class='pollheader'>");
		buffer.append("<td class='pollcell'>Option</td>");
		buffer.append("<td class='pollcell'>Votes</td>");
		buffer.append("</tr>");

		for (PollResult result : thread.getPollResults().getResults()) {
			buffer.append("<tr class='pollrow'>");
			buffer.append("<td class='pollcell'>" + result.getOptionText() + "</td><td class='pollcell'>" + result.getVoteCount() + "</td>");
			buffer.append("</tr>");
		}

		buffer.append("</table>");

		buffer.append("<br /><br />");

		MessagesDisplay.appendUserDetails(key, buffer, thread, message, user, mobileDevice);

		if (user.isAdmin()) {
			buffer.append("<p>");
			MessageAdminLinksDisplay.includeAdminLinks(buffer, thread.getBoardId(), thread.getThreadId(), 1, true, thread.getLoginId(), thread.getIpAddress(), user);
			buffer.append("</p>");
		}

		buffer.append("</div>");

		String body = buffer.toString();

		/* Get rid of any images, if you've set that preference, or are an admin */

		body = ImageHider.substituteImagesForLinks(body, user.isAdmin(), false, (!user.getPreferences().isShowImages()));

		return body;
	}

}
